package org.jetnuke.modules.blog;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogCommentDAO;
import org.jetnuke.dao.BlogPostDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.BlogCategory;
import org.jetnuke.entities.BlogHits;
import org.jetnuke.entities.BlogPost;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.modules.blog.utils.BlogUtils;

public class ShowPage implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ShowPage.class);

	int id;

	public void setId(int id) {
		this.id = id;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		BlogPostDAO bpm = DataAccessDriver.getInst().newBlogPostDAO();
		BlogCategoryDAO bcm = DataAccessDriver.getInst().newBlogCategoryDAO();
		UserDAO um = DataAccessDriver.getInst().newUserDAO();

		BlogPost bp = bpm.retrieveByPK(id);
		if (id != bp.getID())
			return null;
		int aid = bp.getAID();
		vc.put("blog", bp);
		if (!"publish".equals(bp.getPOSTSTATUS())) {
			if (ui == null || ui.getID() != aid) {
				ActionForward returnActionForward = new ActionForward(info,
						"no_page.vm");
				if (logger.isDebugEnabled()) {
					logger
							.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
				}
				return returnActionForward;
			}
		}

		List userBlogCategories = bcm.selectByUser(aid);

		vc.put("lsCategories", userBlogCategories);
		User user = um.selectById(aid);
		vc.put("user", user);

		boolean myBlog = false;
		if (ui != null && ui.getID() == aid) {
			myBlog = true;
			vc.put("my_blog", Boolean.TRUE);
		}
		if (ui != null && ui.getID() > 1) {
			vc.put("author", ui.getUname());
			vc.put("email", ui.getEMAil());
		}

		BlogUserSettingsDAO busm = DataAccessDriver.getInst()
				.newBlogUserSettingsDAO();
		BlogUserSettings bus = busm.retrieveByPk(aid);
		vc.put("settings", bus);

		BlogCommentDAO bcmm = DataAccessDriver.getInst().newBlogCommentDAO();
		List comments = null;
		if (myBlog)
			comments = bcmm.selectAllByPostID(id);
		else {
			BlogHits bh = new BlogHits();
			bh.setAUTHOR_ID(aid);
			bh.setPOST_ID(id);
			bh.setTIMESTAMP(new GregorianCalendar().getTime());
			DataAccessDriver.getInst().newBlogHitsDAO().add(bh);
			comments = bcmm.selectByPostID(id);
		}
		vc.put("comments", comments);

		BlogCategory currentCat = bcm.retrieveByPK(bp.getPOSTCATEGORY());
		vc.put("currentCat", currentCat);

		MainConfig config = (MainConfig) request.getAttribute("MAIN_CONFIG");
		String url = config.getNUKEURL();
		String trackBackURL = url + "do/blog/trackback?id=" + id;
		vc.put("trackBackURL", trackBackURL);
		vc.put("_pagetitle", " - " + bus.getTITLE());

		List preLeftBlocks = new ArrayList();
		preLeftBlocks.add(BlogUtils.createUserBlockInfo(vc, user));
		preLeftBlocks.add(BlogUtils.createCalendarBlock(vc,
				new GregorianCalendar(), aid));
		BlogUtils.manageStyle(bus, vc, preLeftBlocks);

		String pingbackXMLRpc = url + "do/blog/pingback";

		response.addHeader("X-Pingback", pingbackXMLRpc);

		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
